import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public int[] dx = new int[] {0, 0, 1, -1};
    public int[] dy = new int[] {1, -1, 0, 0};
    public boolean[][] vis = new boolean[50][50];
    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int maxArea = 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(grid[i][j] == 1 && !vis[i][j]){
                    //if(grid[i][j] == 1){
                    int tmp = calArea(grid, i, j);
                    if(maxArea < tmp){
                        maxArea = tmp;
                    }
                }
            }
        }
        return maxArea;
    }
    public int calArea(int[][] grid, int i, int j){
        int m = grid.length;
        int n = grid[0].length;
        int area = 0;
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[] {i, j});
        area++;
        vis[i][j] = true;
        //grid[i][j] = 0;
        while(!queue.isEmpty()){
            int[] point = queue.poll();
            int a = point[0];
            int b = point[1];
            for(int k = 0; k < 4; k++){
                int x = a + dx[k];
                int y = b + dy[k];
                if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && !vis[x][y]){
                    //if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1){
                    queue.offer(new int[] {x, y});
                    area++;
                    vis[x][y] = true;
                    //grid[x][y] = 0;
                }
            }
        }
        return area;
    }
}